/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servlet;

import tool.CommonConstants;
import tool.DBUtilities;
import dto.ProductDTO;
import dto.ProductDTOList;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.xml.bind.JAXBException;
import tool.XMLTools;

/**
 *
 * @author DH
 */
public class StartupServlet extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        /*try {
            Connection con = null;
            ResultSet rs = null;

            try {
                con = DBUtilities.createConnection();
                String query = "SELECT TOP " + CommonConstants.NUMBER_PRODUCT + " * FROM [Product] WHERE [isActive] = '1' ORDER BY [addDate] DESC";
                PreparedStatement ps = con.prepareStatement(query);
                rs = ps.executeQuery();

                List prodList = new ArrayList();
                ProductDTO prod = null;

                if (rs != null) {
                    while (rs.next()) {
                        int id = rs.getInt("id");;
                        String productName = rs.getString("productName");
                        String productDescription = rs.getString("productDescription");
                        int productCategoryId = rs.getInt("productCategoryId");
                        int price = rs.getInt("price");
                        String imagePath = rs.getString("imagePath");
                        Date addDate = rs.getTimestamp("addDate");
                        prod = new ProductDTO(id, productName, productDescription, productCategoryId, price, imagePath, addDate, true);
                        prodList.add(prod);
                    }
                }

                HttpSession ss = request.getSession();
                ss.setAttribute("PROD", prodList);
                response.sendRedirect("index.jsp");

            } catch (ClassNotFoundException ex) {
                ex.printStackTrace();
            } catch (SQLException ex) {
                ex.printStackTrace();
            } finally {
                try {
                    if (con != null) {
                        con.close();
                    }
                    if (rs != null) {
                        rs.close();
                    }
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
            }
        } finally {
            out.close();
        }*/
        try {
            Connection con = null;
            ResultSet rs = null;

            try {
                con = DBUtilities.createConnection();
                String query = "SELECT * FROM [Product] WHERE [isActive] = '1'";
                PreparedStatement ps = con.prepareStatement(query);
                rs = ps.executeQuery();

                ProductDTOList products = new ProductDTOList();
                ProductDTO prod = null;

                if (rs != null) {
                    while (rs.next()) {
                        int id = rs.getInt("id");;
                        String productName = rs.getString("productName");
                        String productDescription = rs.getString("productDescription");
                        int productCategoryId = rs.getInt("productCategoryId");
                        int price = rs.getInt("price");
                        String imagePath = rs.getString("imagePath");
                        Date addDate = rs.getTimestamp("addDate");
                        prod = new ProductDTO(id, productName, productDescription, productCategoryId, price, imagePath, addDate, true);
                        products.getProduct().add(prod);
                    }
                    String realPath = getServletContext().getRealPath("/");
                    String fileXMLPath = realPath + "/products.xml";
                    XMLTools.JAXBMarshalling(products, fileXMLPath);
                    response.sendRedirect("index.jsp");
                }


            } catch (JAXBException ex) {
                ex.printStackTrace();
            } catch (ClassNotFoundException ex) {
                ex.printStackTrace();
            } catch (SQLException ex) {
                ex.printStackTrace();
            } finally {
                try {
                    if (con != null) {
                        con.close();
                    }
                    if (rs != null) {
                        rs.close();
                    }
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
            }
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
